package com.android.update;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.alibaba.fastjson.JSON;
import com.android.update.data.FilterInfo;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.android.update.config.HookConfig;
import com.android.update.config.HookDB;
import com.android.update.R;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private Button button;
    private TextView tvContent, tvFilter, tvBeFiltered;
    private ToggleButton tgPer, tgUp, tgTest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String carrierStr = android.os.Build.MANUFACTURER;
        button = (Button) findViewById(R.id.btnCommit);
        tvContent = (TextView) findViewById(R.id.tvContent);
        tvFilter = (TextView) findViewById(R.id.tvFilter);
        tvBeFiltered = (TextView) findViewById(R.id.tvBeFiltered);
        tgPer = (ToggleButton) findViewById(R.id.tgPer);
        tgUp = (ToggleButton) findViewById(R.id.tgUp);
        tgTest = (ToggleButton) findViewById(R.id.tgTest);

        tgPer.setChecked(HookConfig.getIntance().isPermissions());
        tgUp.setChecked(HookConfig.getIntance().isActivate());
        tgTest.setChecked(HookConfig.getIntance().isTest());

        tgPer.setOnCheckedChangeListener(this);
        tgUp.setOnCheckedChangeListener(this);
        tgTest.setOnCheckedChangeListener(this);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HookDB.getIntance().saveData(tvContent.getText().toString());
                HookDB.getIntance().saveFilter(tvFilter.getText().toString());
                HookDB.getIntance().saveBeFiltered(tvBeFiltered.getText().toString());
            }
        });
        findViewById(R.id.btnGet).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getNetConfig();
            }
        });
        getNetConfig();


    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView == tgPer) {
            HookConfig.getIntance().savePermissions(isChecked);
            return;
        }
        if (buttonView == tgUp) {
            HookConfig.getIntance().saveActivate(isChecked);
            return;
        }
        if (buttonView == tgTest) {
            HookConfig.getIntance().saveTest(isChecked);
            return;
        }
    }

    private void getNetConfig() {
        AQuery aq = new AQuery(MainActivity.this);
        aq.ajax("http://duf.17huiben.com/app/act", String.class, new AjaxCallback<String>() {
            @Override
            public void callback(String url, String object, AjaxStatus status) {
                super.callback(url, object, status);
                if (object != null) {
                    Toast.makeText(MainActivity.this, "从服务器获取成功", Toast.LENGTH_SHORT).show();
                    System.out.println("请求成功>> " + JSON.toJSONString(object));
                    tvContent.setText(object);
                    HookDB.getIntance().saveData(tvContent.getText().toString());
                }
            }
        });

        aq.ajax("http://duf.17huiben.com/app/filter", String.class, new AjaxCallback<String>() {
            @Override
            public void callback(String url, String object, AjaxStatus status) {
                super.callback(url, object, status);
                if (object != null) {
                    Toast.makeText(MainActivity.this, "从服务器获取过滤成功", Toast.LENGTH_SHORT).show();
                    FilterInfo info = JSON.parseObject(object, FilterInfo.class);
                    HookDB.getIntance().saveBeFiltered(info.beFiltered);
                    HookDB.getIntance().saveFilter(info.filter);
                    tvFilter.setText(info.filter);
                    tvBeFiltered.setText(info.beFiltered);
                }
            }
        });
    }

}
